package com.ddpt.permission.dubbo;

import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lh.center.common.base.BizEntity;
import com.lh.center.common.base.ResponseEntity;
import com.lh.center.common.constant.GlobalConstant;
import com.lh.center.common.dto.MerchantIdDto;
import com.lh.center.common.exception.CommonException;
import com.lh.center.common.exception.ErrorEnum;
import com.lh.center.permission.api.dubbo.MerchantAdminUserService;
import com.lh.center.permission.api.entity.MerchantAdminUser;
import com.lh.center.permission.api.request.MerchantAdminUserQueryRequest;
import com.lh.center.permission.api.request.MerchantEmpCountQueryRequest;
import com.lh.center.permission.api.request.UpdateBindWxPromptStatusReq;
import com.lh.center.permission.api.request.UpdateMerchantUserPasswordReq;
import com.lh.center.permission.api.response.MerchantAdminUserResponse;
import com.lh.center.permission.api.response.MerchantEmpCountResponse;
import com.lh.center.permission.api.response.MerchantInsEmpCountResponse;
import com.lh.center.permission.mapper.MerchantAdminUserMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collection;
import java.util.List;

@DubboService
public class DubboMerchantAdminUserServiceImpl implements MerchantAdminUserService {

    @Autowired
    private com.lh.center.permission.service.MerchantAdminUserService merchantAdminUserService;

    @Autowired
    private MerchantAdminUserMapper merchantAdminUserMapper;

    @Override
    public void createMerchantAdmin(MerchantAdminUser adminUser) {
        merchantAdminUserService.createMerchantAdmin(adminUser);
    }

    @Override
    public ResponseEntity<List<MerchantEmpCountResponse>> queryMerchantEmpCount(MerchantEmpCountQueryRequest queryRequest) {
        return merchantAdminUserService.queryMerchantEmpCount(queryRequest);
    }

    @Override
    public void resetPasswordFromBack(MerchantAdminUser adminUser) {
        merchantAdminUserService.resetPasswordFromBack(adminUser);
    }

    @Override
    public List<MerchantInsEmpCountResponse> queryEmpCount(List<Long> insIds) {
        return merchantAdminUserMapper.queryEmpCount(insIds);
    }

    @Override
    public Page<MerchantAdminUserResponse> queryPageList(MerchantAdminUserQueryRequest queryRequest) {
        merchantAdminUserMapper.queryPageList(queryRequest);
        return queryRequest;
    }

    @Override
    public List<MerchantAdminUserResponse> queryList(MerchantIdDto queryRequest) {
        return merchantAdminUserMapper.queryList(queryRequest);
    }

    @Override
    public List<MerchantAdminUser> list(Collection<Long> userIds) {
        return merchantAdminUserService.list(Wrappers.lambdaQuery(MerchantAdminUser.class).in(MerchantAdminUser::getId, userIds));
    }

    @Override
    public void createNormalAdminUser(MerchantAdminUser adminUser) {
        merchantAdminUserService.checkAdminUsernameUnique(null, adminUser.getUsername());
        adminUser.setType(1);
        adminUser.setPassword(DigestUtil.md5Hex(adminUser.getPassword()));
        merchantAdminUserService.save(adminUser);
    }

    @Override
    public void updatePassword(UpdateMerchantUserPasswordReq updateMerchantUserPasswordReq) {
        merchantAdminUserService.updatePassword(updateMerchantUserPasswordReq);
    }

    @Override
    public MerchantAdminUser getMerchanAdminUser(Long userId) {
        return merchantAdminUserService.getMerchanAdminUser(userId);
    }

    @Override
    public void bindWx(Long userId, String openId) {
        MerchantAdminUser user = merchantAdminUserService.getMerchanAdminUser(userId);
        user.setBindWechat(1);
        user.setWxOpenId(openId);
        merchantAdminUserService.updateById(user);
    }

    @Override
    public void unBindWx(Long id) {
        LambdaUpdateWrapper<MerchantAdminUser> wrapper = Wrappers.lambdaUpdate(MerchantAdminUser.class).eq(MerchantAdminUser::getId, id).set(MerchantAdminUser::getWxOpenId, null).set(MerchantAdminUser::getBindWechat, 0);
        merchantAdminUserService.update(wrapper);
    }

    @Override
    public MerchantAdminUser getByUsername(String name) {
        MerchantAdminUser user = merchantAdminUserService.get(Wrappers.lambdaQuery(MerchantAdminUser.class)
                .eq(MerchantAdminUser::getUsername, name));
        return user;
    }

    @Override
    public MerchantAdminUser getByMerchantId(Long merchantId) {
        return merchantAdminUserService.getOne(Wrappers.lambdaQuery(MerchantAdminUser.class)
                .eq(BizEntity::getMerchantId,merchantId)
                .eq(MerchantAdminUser::getType,GlobalConstant.ZERO));
    }

    @Override
    public List<MerchantAdminUser> getByMerchantIds(Collection<Long> merchantIds) {
        return merchantAdminUserService.list(Wrappers.lambdaQuery(MerchantAdminUser.class)
                .in(MerchantAdminUser::getMerchantId,merchantIds)
                .eq(MerchantAdminUser::getType,GlobalConstant.ZERO));
    }


    @Override
    public MerchantAdminUserResponse queryDetail(Long id) {
        return merchantAdminUserMapper.queryDetail(id);
    }

    @Override
    public void updateAdmin(MerchantAdminUser adminUser) {
        MerchantAdminUser oldOne = merchantAdminUserService.getById(adminUser.getId());
        if(GlobalConstant.ZERO.equals(oldOne.getType())) {
            throw new CommonException(ErrorEnum.MERCHANT_ADMIN_CANNOT_EDIT);
        }
        adminUser.setUsername(null);
        String password = adminUser.getPassword();
        if (StringUtils.isNotBlank(password)) {
            adminUser.setPassword(DigestUtil.md5Hex(password));
        } else {
            adminUser.setPassword(null);
        }
        merchantAdminUserService.updateById(adminUser);
    }

    @Override
    public void updateBindWxPromptStatus(UpdateBindWxPromptStatusReq updateBindWxPromptStatusReq) {
        MerchantAdminUser user = merchantAdminUserService.getById(updateBindWxPromptStatusReq.getId());
        user.setBindWxPromptStatus(updateBindWxPromptStatusReq.getBindWxPromptStatus());
        merchantAdminUserService.updateById(user);
    }

    @Override
    public List<MerchantAdminUser> queryMerchantByMerchantId(Long merchantId,Long insId){
        return merchantAdminUserService.list(new LambdaQueryWrapper<MerchantAdminUser>()
                .eq(BizEntity::getMerchantId,merchantId)
                .eq(MerchantAdminUser::getInsId,insId)
        );
    }
}
